import service from "../index.js";

// encapsulate the user api request

// i think personally it's better do not encapsulate the api request in a function
// but just use the api directly in the component 
// and handle the response in the component.
export function addUser(name,password) {
    // springboot backend
    // @PostMapping("/addUser")
    // //    @GetMapping("/addUser")
    // public String addUser(@RequestBody User user){
    //     System.out.println(user);
    //     userMapper.insertUser(user);
    //     return "success";
    // }

    // 
    var data = JSON.stringify({
        "name": name,
        "password": password
        // "name": addInfo.value.name,
        // "password": addInfo.value.password
    });
    if (addInfo.value.name === "" || addInfo.value.password === null) {
        alert('name or password is empty generate random')
        return;
    }
    service.post('/api/addUser', data, 
        {
            'Content-Type': 'application/json'
        }
    )
    .then(function (response) {
        getAllUser()
    })
    .catch(function (error) {
        console.log(error);
    });
}
